using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using RaidLog.Engine.DAL;
using RaidLog.Engine.LogParser;

namespace RaidLog.WebService
{
    [WebService(Namespace = "http://asp.recursionursionursion.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class ImportDataSrv : System.Web.Services.WebService
    {
        WowDatabase _DB = null;

        public ImportDataSrv()
        {
            System.Configuration.Configuration rootWebConfig = 
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
            
            System.Configuration.ConnectionStringSettings connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["RaidLog.Engine.Properties.Settings.RaidLogConnectionString"];
            
            System.Data.SqlClient.SqlConnectionStringBuilder connectionString = 
                new System.Data.SqlClient.SqlConnectionStringBuilder(connString.ConnectionString);
            
            _DB = new WowDatabase(connectionString);
        }

        [WebMethod]
        public RaidData GetRaids()
        {
            return _DB.GetAllRaids();
        }

        [WebMethod]
        public void RegenerateAnalysisData(int raidID)
        {
            _DB.RegenerateAnalysis(raidID);
        }

        [WebMethod]
        public LootData GetRaidLootData(int raidID)
        {
            return _DB.GetLootCredit(raidID);
        }

        [WebMethod]
        public void CreateRaid(DateTime start, DateTime end, string instance)
        {
            _DB.CreateRaid(start, end, instance);
        }

        [WebMethod]
        public void AppendLogData(int raidID, string name, string[] data)
        {
            LogParser<ImportData> parser = new LogParser<ImportData>("CombatLog.xml");
            foreach(string logline in data)
            {
                parser.ParseLine(logline);
            }
            _DB.ImportData(parser.Data, raidID, name);
        }

        [WebMethod]
        public void InitializeDatabase()
        {
            _DB.InitializeDatabase();
        }
    }
}
